﻿using IBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.XSSF.UserModel;
namespace We.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MedicationController : ControllerBase, IMedicationbLL
    {
          public MedicationController(IMedicationbLL bll,ILogger<MedicationController> logger)
        {
            Bll = bll;
            Logger = logger;
        }

        public IMedicationbLL Bll { get; }
        public ILogger<MedicationController> Logger { get; }

        [HttpPost]
        public Task<int> AddAnync(Medication medicati)
        {
       
              return Bll.AddAnync(medicati);
        }

        [HttpGet]
        public Medication FindMedication(int id)
        {
             return Bll.FindMedication(id);
        }

        [HttpPost]
        public List<Medication> GetMedications()
        {
            
             return Bll.GetMedications();
        }

        [HttpGet]
        public Task<int> IsDeleteState(int id)
        {
             return Bll.IsDeleteState(id);
        }

        [HttpDelete]
        public Task<int> PudataStete(string idstrings)
        {
             return Bll.PudataStete (idstrings);
        }

        [HttpGet]
        public Dto ShowMedication(string? name, int departmentId, int index, int size)
        {
            return Bll.ShowMedication(name, departmentId, index, size);

        }

        [HttpPost]
        public Task<int> UpadateAnync(Medication medicati)
        {
            try
            {
                Logger.LogError($"{medicati}获取数据错误");
                return Bll.UpadateAnync(medicati);
            }
            catch (Exception)
            {

         
                throw;
            }
           
        }


        [HttpPost]
         public IActionResult Exec()
        {
            List<Medication> Shent = Bll.GetMedications();

            XSSFWorkbook sheets = new XSSFWorkbook();
            var shs = sheets.CreateSheet("用药信息");
             var row= shs.CreateRow(0);

            row.CreateCell(0).SetCellValue("id");
            row.CreateCell(1).SetCellValue("药品名称");
            row.CreateCell(2).SetCellValue("科室名称");
            row.CreateCell(3).SetCellValue("用药日期");
            row.CreateCell(4).SetCellValue("病人名称");
            row.CreateCell(5).SetCellValue("病房号码");
            row.CreateCell(6).SetCellValue("配药医生");
            row.CreateCell(7).SetCellValue("状态");
            row.CreateCell(8).SetCellValue("删除");

            int num = 1;
             foreach (Medication item in Shent)
            {
                var row1 = shs.CreateRow(num);

                row1.CreateCell(0).SetCellValue(item.MedicationId);
                row1.CreateCell(1).SetCellValue(item.MedicationName);
                row1.CreateCell(2).SetCellValue(item.DepartmentId);
                row1.CreateCell(3).SetCellValue(item.MedicationDate);
                row1.CreateCell(4).SetCellValue(item.Name);
                row1.CreateCell(5).SetCellValue(item.MedicationPhone);
                row1.CreateCell(6).SetCellValue(item.Doctor);
                row1.CreateCell(7).SetCellValue(item.State==true?"已完成":"未完成");
                row1.CreateCell(8).SetCellValue(item.isDelete==true?"已删除":"未删除");

                num++;
            }

              MemoryStream memory = new MemoryStream();

            sheets.Write(memory);
            byte[] data = memory.ToArray();
            var mime = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(data, mime, ("用药信息.xlsx"));







        }
    }
}
